package com.woniuxy.consumer.student.controller;

import com.woniuxy.common.DTO.ResponseResult;
import com.woniuxy.common.entity.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/1/20 9:48
 * @FileName: ConsumerStudentController
 * @version: 1.0
 * @Description: 学生消费服务调用类
 * 远程调用8081项目
 *
 * 原始调用方式：RestTemplate调用
 */
@Api(tags = {"消费者","调用方"},value = "调用学生类")
@RestController
@RequestMapping("/consumerStudent")
public class ConsumerStudentController {

    @Autowired
    RestTemplate restTemplate;

    final String URL = "HTTP://localhost:8081/student/";

    @GetMapping("/{id}")
    public ResponseResult<Student> getStudentById(@PathVariable("id")int id) {
        // restTemplate的API 实现远程调用
        return restTemplate.getForObject(URL+id, ResponseResult.class);
    }

    @GetMapping("/")
    public ResponseResult<List<Student>> getStudentAll() {
        // restTemplate的API 实现远程调用
        return restTemplate.getForObject(URL, ResponseResult.class);
    }

    @PostMapping("/")
    public ResponseResult<List<Student>> addStudent(@RequestBody Student student) {
        // restTemplate的API 实现远程调用
        return restTemplate.postForObject(URL,student, ResponseResult.class);
    }

    @ApiOperation("分页查询")
    @GetMapping("/{pageIndex}/{pageSize}")
    public ResponseResult<List<Student>> getStudentPage(@PathVariable("pageIndex") int pageIndex,
                                                        @PathVariable("pageSize") int pageSize,
                                                        @RequestBody Student student) {
        // restTemplate的API 实现远程调用
        return restTemplate.getForObject(URL, ResponseResult.class);
    }
}
